package com.songcha.library_business.api

import android.os.Build
import android.util.Log
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.BuildConfig
import com.songcha.library_common.LibraryCommon
import com.songcha.library_common.util.DeviceUtil
import com.songcha.library_common.util.PackageUtil
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

class MyInterceptor : Interceptor {
    private var versionName=""
    private var channel=""
    private var packageName=""
    private var deviceId=""
    private var deviceSystem=""
    private var deviceBrand=""
    private var deviceModel=""
    private var accessDeviceEnabled = false

    companion object{
        @Volatile
        private var instance :MyInterceptor?=null

        fun getInstance():MyInterceptor{
            if(instance == null){
                synchronized(MyInterceptor::class.java){
                    if(instance == null){
                        instance = MyInterceptor()
                    }
                }
            }
            return instance!!
        }
    }

    private constructor()

    fun setAccessDeviceEnabled(enabled:Boolean){
        accessDeviceEnabled = enabled
    }


    override fun intercept(chain: Interceptor.Chain): Response {
        if(versionName.isEmpty()){
            versionName = PackageUtil.getPackageVersionName(LibraryCommon.getContext())
        }
        if(channel.isEmpty()){
            channel = PackageUtil.getUmengChannelName(LibraryCommon.getContext())
        }
        if(packageName.isEmpty()){
            packageName = PackageUtil.getPackageName(LibraryCommon.getContext())
        }
        if(deviceId.isEmpty() && accessDeviceEnabled){
            deviceId = DeviceUtil.getAndroidId(LibraryCommon.getContext())
        }


        if(deviceSystem.isEmpty() && accessDeviceEnabled){
            deviceSystem = Build.VERSION.RELEASE
        }
        if(deviceBrand.isEmpty() && accessDeviceEnabled){
            deviceBrand = Build.BRAND
        }
        if(deviceModel.isEmpty() && accessDeviceEnabled){
            deviceModel = Build.MODEL
        }

        val originalRequest:Request = chain.request()

        var hostName = ""
        if(packageName.isNotEmpty()){
            hostName = packageName.replace("com.","").replace(".","_")
        }

        val requestBuilder:Request.Builder = originalRequest.newBuilder()
            .addHeader("content-type","application/json")
            .addHeader("versionname",versionName)
            .addHeader("channel",channel)
            .addHeader("packagename",packageName)
            .addHeader("device-id",deviceId)
            .addHeader("device-platform","android")
            .addHeader("device-system",deviceSystem)
            .addHeader("device-brand",deviceBrand)
            .addHeader("device-model",deviceModel)
            .addHeader("host-version",versionName)
            .addHeader("host-app-name",hostName)
            .addHeader("token",UserInfoManager.loginToken)

        val newRequest = requestBuilder.build()
        val response = chain.proceed(newRequest)
        Log.d("token数据",""+UserInfoManager.loginToken)
        return response

    }

}